package cn.wolfcode.web.controller;

import cn.wolfcode.transfer.domain.Employee;
import cn.wolfcode.transfer.domain.LoginNotice;
import cn.wolfcode.transfer.domain.Notice;
import cn.wolfcode.transfer.domain.Permission;
import cn.wolfcode.transfer.qo.JsonResult;
import cn.wolfcode.transfer.service.IEmployeeService;
import cn.wolfcode.transfer.service.INoticeService;
import cn.wolfcode.transfer.service.IPermissionService;
import cn.wolfcode.transfer.util.UserContext;
import com.fasterxml.jackson.annotation.JsonAlias;
import org.apache.ibatis.annotations.ResultMap;
import org.aspectj.lang.annotation.After;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.annotation.RequestScope;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class loginController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IPermissionService permissionService;
    @Autowired
    private INoticeService noticeService;

    @RequestMapping("/emplogin")
    @ResponseBody
    public JsonResult login(String username , String password ){
            Employee employee = employeeService.login(username,password);
        LoginNotice loginNotice = noticeService.loginByIdGetNotice(employee.getId());
        UserContext.setNotice(loginNotice);
        UserContext.setCurrentUser(employee);
            if (!employee.getAdmin()) {
                List<String> expression = permissionService.selectByEmployeeId(employee.getId());
                UserContext.setExpressions(expression);
            }
        return new JsonResult();
    }
    @RequestMapping("/loginOut")
    public String loginOut(HttpSession session){
        session.invalidate();
        return "redirect:/login.html";
    }
    @RequestMapping("/nopermission")
    public String nopermission(){
        return "/common/nopermission";
    }

}
